/**
 * 
 */
package com.yidu.action.emp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yidu.common.Pages;
import com.yidu.common.SsmMessage;
import com.yidu.common.Tools;
import com.yidu.domain.Emp;
import com.yidu.service.emp.EmpService;

/**
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("emp")
public class EmpAction {
	@Resource
	private EmpService empService ;
	
	@RequestMapping("/validateName")
	@ResponseBody
	public SsmMessage validateName(String empName){
		SsmMessage ssm = new SsmMessage();
		boolean flag = empService.validateName(empName);
		if(flag){
			ssm.setState(1);
			ssm.setMes("恭喜,该账号可登录!");
		}else{
			ssm.setState(0);
			ssm.setMes("不存在该账号");
		}
		return ssm;
	}
	
	@RequestMapping("/login")
	public ModelAndView login(String empName,String empDesc){
		ModelAndView view = new ModelAndView("index");
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("empName", empName);
		map.put("empDesc", empDesc);
		
		boolean flag =empService.findLogin(map);
		if(!flag){
			view.addObject("info", "账号密码错误!");
			view.setViewName("emp/login");
		}else{
			view.setViewName("redirect:showList.action");
			
		}
		return view ;
	}
	
	@RequestMapping("/showList")
	public ModelAndView showList(String curPage,String keywords,String start,String end){
		Map<String,Object> mapParam = new HashMap<>();
		mapParam.put("keywords", keywords);
		mapParam.put("start", start);
		mapParam.put("end", end);
		
		
		System.out.println("keywords==============="+keywords+"    "+start+"     "+end);
		ModelAndView view = new ModelAndView("index");
		Pages pages = new Pages();
		int totalRows = empService.findAllRowCount(mapParam);//得到总行数
		pages.setTotalRows(totalRows);
		pages.setMaxResult(Tools.MAX_RESULT);
		if(curPage==null){
			pages.setCurPage(1);
		}else{
			pages.setCurPage(Integer.valueOf(curPage));
		}
		
		
		mapParam.put("firstRows", pages.getFirstRows());
		mapParam.put("maxResult", pages.getMaxResult());
		
		
		List<Emp> list = empService.selectAll(mapParam);
		view.addObject("empList",list);
		view.addObject("pages",pages);
		view.addObject("keywords",keywords);
		System.out.println(list.size()+"===============");
		return view ;
	}
	
	@RequestMapping("/showList2")
	public ModelAndView showList2(String curPage){
		ModelAndView view = new ModelAndView("index");
		Pages pages = new Pages();
		pages.setMaxResult(Tools.MAX_RESULT);
		if(curPage==null){
			pages.setCurPage(1);
		}else{
			pages.setCurPage(Integer.valueOf(curPage));
		} 
		System.out.println(pages.getFirstRows()+"    "+pages.getMaxResult());
		 
		//PageHelper.startPage(1,5);
		 
		List<Emp> list = empService.selectAll2();
		
		long count = empService.findAllRowCount(null);//得到总行数
		System.out.println("==============="+count); 
	    pages.setTotalRows((int)count);
		
		view.addObject("empList",list);
		view.addObject("pages",pages);
		
		return view ;
	}
	
	
	@RequestMapping("/delete")
	public ModelAndView delete(String id){
		ModelAndView view = new ModelAndView("redirect:showList.action");
		int row = empService.deleteByPrimaryKey(id);
		if(row>0){
			return view ;
		}else{
			view.addObject("info", "删除人员失败!");
			view.setViewName("error");
			return view ;
		} 
	}
	
	
	@RequestMapping("/showForm")
	public ModelAndView showForm(String id){
		//如果id为空,则增加,如果id不为空,则修改
		ModelAndView view = new ModelAndView("emp/form");
		
		return view ;
	}
	
	
	@RequestMapping("/addOrUpdate")
	public ModelAndView addOrUpdate(Emp emp){
		ModelAndView view = new ModelAndView("succss");
		
		int row = empService.insertSelective(emp);
		if(row>0){
			view.addObject("state", "1");
			view.addObject("info", "增加人员成功!");
		}else{
			view.addObject("state", "0");
			view.addObject("info", "增加人员失败!");
		}
		return view ;
		
	}
	
	/**
	 * 使用ajax方式
	 * @param emp
	 * @return
	 */
	@RequestMapping("/addOrUpdateAjax")
	@ResponseBody
	public SsmMessage addOrUpdateAjax(Emp emp){
		SsmMessage ssm = new SsmMessage();
		int row = 0;
		if(Tools.isEmpty(emp.getEmpId())){
			row =empService.insertSelective(emp);
		}else{
			row =empService.updateByPrimaryKeySelective(emp);
		}
		
		
		if(row>0){
			ssm.setState(1);
			ssm.setMes("增加人员成功");
		}else{
			ssm.setState(0);
			ssm.setMes("增加人员失败");
		}
		return ssm ;
		
	}
	
	
	/**
	 * 使用ajax方式
	 * @param emp
	 * @return
	 */
	@RequestMapping("/showUpdate")
	@ResponseBody
	public Emp showUpdate(String id){
		Emp ssm = new Emp();
		if(Tools.isEmpty(id)) return ssm ;
		
		ssm = empService.selectByPrimaryKey(id); 
		return ssm ;
		
	}

}
